﻿/***
    This file is part of Qasparov.

    Qasparov is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Qasparov is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Qasparov. If not, see <http://www.gnu.org/licenses/>.

 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.Oxilly.Qsb.Core.SDK.EF;

namespace Com.Oxilly.Qsb.Core.QBusConnector
{
    public class QBusQApp : QAppBase
    {
        private QBusManager qBusManager;
        private SDK.QsbEngineApi api;

        #region QApp Life cycle
        protected override void OnStart(SDK.QsbEngineApi api)
        {
            this.api = api;
            this.qBusManager = new QBusManager();
          
        }

        protected override void OnActivate(Com.Oxilly.Qsb.Core.SDK.QsbEngineApi api)
        {
            this.qBusManager.ConnectionChanged += new EventHandler<Events.ConnectionChangedEventArg>(qBusManager_ConnectionChanged);
            this.qBusManager.ModuleValueChanged += new EventHandler<ModuleValueChangedEventArg>(qBusManager_ModuleValueChanged);

            this.qBusManager.Connect();
            
        }

        void qBusManager_ModuleValueChanged(object sender, ModuleValueChangedEventArg e)
        {
            var msg = new SDK.API.QsbMessage
            {
                MessageType = "Qasparov.QBus.ModuleValueChanged",
                MessageSender = this.GetType().FullName,
                Description = String.Format("Module {0} value changed.",
                    e.Module.Name)
            };
            msg.MessageAttributes.Add("ModuleName", e.Module.Name);

            this.api.ProcessQsbMessage(msg);
        }

        void qBusManager_ConnectionChanged(object sender, Events.ConnectionChangedEventArg e)
        {
            var msg = new SDK.API.QsbMessage
            {
                MessageType = "Qasparov.QBus.ConnectionChanged",
                MessageSender = this.GetType().FullName,
                Description = String.Format("Connection {1} changed to {0}.",
                        e.Connected,
                        e.Connection.Controller.Address),
            };
            msg.MessageAttributes.Add("Connection", e.Connection.Controller.Address);
            msg.MessageAttributes.Add("Connected", e.Connected.ToString());
            msg.MessageAttributes.Add("Controller", e.Connection.Controller.Name);

            this.api.ProcessQsbMessage(msg);
        }

        #endregion

        protected override bool Filter(Com.Oxilly.Qsb.Core.SDK.QsbEngineApi api, Com.Oxilly.Qsb.Core.SDK.Events.QsbEventArgs e)
        {
            return false;
        }

        protected override void OnQsbEvent(Com.Oxilly.Qsb.Core.SDK.QsbEngineApi api, Com.Oxilly.Qsb.Core.SDK.Events.QsbEventArgs e)
        {

        }


        protected override void OnQsbMessageReceived(Com.Oxilly.Qsb.Core.SDK.QsbEngineApi api, Com.Oxilly.Qsb.Core.SDK.API.QsbMessage message)
        {

        }

        protected override void OnReleaseResources()
        {
            
        }
    }
}
